﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Ledict.ConditionExtract.Controls
{
    public partial class SelectWindow : UserControl
    {

        public string Names { get; set; }
        private List<gridViewColumn> m_GridViewList = new List<gridViewColumn>();
        private LinkLabel m_addbtn = null;

        public SelectWindow()
        {
            InitializeComponent();
            CreateGridViewData(Names);

            #region 添加右键菜单
            ContextMenuStrip contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip();

            ToolStripMenuItem 清空ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();

            // 
            // 清空ToolStripMenuItem
            // 
            清空ToolStripMenuItem.Name = "清空ToolStripMenuItem";
            清空ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            清空ToolStripMenuItem.Text = "清空";
            清空ToolStripMenuItem.Click += new System.EventHandler(this.清空ToolStripMenuItem_Click);
            contextMenuStrip1.Items.Add(清空ToolStripMenuItem);

            contextMenuStrip1.Name = "contextMenuStrip1";
            contextMenuStrip1.Size = new System.Drawing.Size(153, 92);
            this.ContextMenuStrip = contextMenuStrip1;
            #endregion
        }

       

        public SelectWindow(string names)
        {
            InitializeComponent();
            Names = names;
            CreateGridViewData(Names);

            #region 添加右键菜单
            ContextMenuStrip contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip();

            ToolStripMenuItem 清空ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
           
            // 
            // 清空ToolStripMenuItem
            // 
            清空ToolStripMenuItem.Name = "清空ToolStripMenuItem";
            清空ToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            清空ToolStripMenuItem.Text = "清空";
            清空ToolStripMenuItem.Click += new System.EventHandler(this.清空ToolStripMenuItem_Click);
            contextMenuStrip1.Items.Add(清空ToolStripMenuItem);
              
            contextMenuStrip1.Name = "contextMenuStrip1";
            contextMenuStrip1.Size = new System.Drawing.Size(153, 92);
            this.ContextMenuStrip = contextMenuStrip1;
            #endregion

        }

        private void CreateGridViewData(string names)
        {
          
            gridViewColumn gvc = new gridViewColumn(names);
            gvc.Left = 0;
            gvc.Top = m_GridViewList.Count * (30 + 2);
            gvc.Width = 500;
            gvc.Height = 30;
            gvc.DeleteBtnClicked += new gridViewColumn.DeleteButtonClick(gridView_deleteButtonClick);
            gridViewColumn1.Controls.Add(gvc);
            m_GridViewList.Add(gvc);

            if (m_addbtn == null)
            {
                m_addbtn = new LinkLabel();
                m_addbtn.Text = "添加";
                Graphics graph = CreateGraphics();
                SizeF sizeF = graph.MeasureString(m_addbtn.Text, this.Font);
                m_addbtn.Width = (int)(Math.Ceiling(sizeF.Width)) + 1;
                m_addbtn.Height = (int)(Math.Ceiling(sizeF.Height));
                m_addbtn.Left = 0;
                m_addbtn.LinkClicked += new LinkLabelLinkClickedEventHandler(this.colLinkButton_Click);
                gridViewColumn1.Controls.Add(m_addbtn);
            }
            m_addbtn.Top = m_GridViewList.Count * (30 + 2);
        }

        private void colLinkButton_Click(object sender, EventArgs e)
        {
            int ctlCount = gridViewColumn1.Size.Height / (30 + 2) - 1;
            if (m_GridViewList.Count <= ctlCount)
            {
                CreateGridViewData(Names);
            }
        }

        /// <summary>
        /// 删除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        private void gridView_deleteButtonClick(object sender)
        {
            gridViewColumn gv = sender as gridViewColumn;
            for (int i = m_GridViewList.Count - 1; i >= 0; i--)
            {
                if (gv == m_GridViewList[i])
                {
                    m_GridViewList.RemoveAt(i);
                    gridViewColumn1.Controls.Remove(gv);
                    break;
                }
            }
            Refesh();
        }

        /// <summary>
        /// 刷新按钮事件
        /// </summary>
        private void Refesh()
        {
            for (int i = 0; i < m_GridViewList.Count; i++)
            {
                gridViewColumn gv = m_GridViewList[i];
                gv.Top = i * (30 + 2);
            }
            m_addbtn.Top = m_GridViewList.Count * (30 + 2);
        }


        /// <summary>
        /// 添加按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 删除按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 删除ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// 清空按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void 清空ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < m_GridViewList.Count; i++)
            {
                gridViewColumn gvc = m_GridViewList[i];
                gridViewColumn1.Controls.Remove(gvc);
            }
            m_GridViewList.Clear();
        }

        public string GetDataString()
        {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < m_GridViewList.Count; i++)
            {
                gridViewColumn gvc = m_GridViewList[i];
                string str= gvc.GetCondition();

                if (!string.IsNullOrEmpty(str) && str != "请添加完整信息")
                {
                    if (i == m_GridViewList.Count - 1)
                    { sb.Append(" " + str.Replace("and", "").Replace("or", "") + " "); }
                    else
                    {
                        sb.Append(" " + str + " ");
                    }
                }
                else if (str == "请添加完整信息")
                {
                    return "请添加完整信息";
                }
                
            }
            return sb.ToString();
        }
    }
}
